package Client;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Vector;

public final class WorldController {
    private static Vector<GameObject> groundTiles = new Vector<GameObject>();
    private static Vector<GameObject> gameObjects = new Vector<GameObject>();

    public static Vector<GameObject> groundTiles() {
        return groundTiles;
    }

    public static Vector<GameObject> gameObjects() {
        return gameObjects;
    }

    public static boolean solidObjectExists(int x, int y) {
        for (GameObject go : gameObjects) {
            if (go.type() == 2 && go.x() == x && go.y() == y) {
                return true;
            }
        }
        return false;
    }

    public static void addGameObject(GameObject go) {
        if (go.type() == 0) {
            groundTiles.add(go);
        } else {
            gameObjects.add(go);
        }
    }

    public static void loadMaps(int regionX, int regionY) {
        try {
            groundTiles.clear();
            gameObjects.clear();

            for (int x2 = regionX - 6 / 8; x2 <= regionX + 6 / 8; x2++) {
                for (int y2 = regionY - 6 / 8; y2 <= regionY + 6 / 8; y2++) {
                    int mapId = (x2 << 8) + y2;
                    int baseX = (mapId >> 0x8) * 64;
                    int baseY = (mapId & 0xff) * 64;
                    File f = new File("./cache/maps/" +mapId + ".dat");
                    ByteStream in = new ByteStream((int) f.length());
                    in.setLength((int) f.length());
                    FileInputStream in2 = new FileInputStream(f);
                    in2.read(in.buffer(), 0, (int) f.length());
                    in2.close();
                    int objectId;
                    while ((objectId = in.getUnsignedShort()) != 65535) {
                        int xOffset;
                        while ((xOffset = in.getUnsignedByte()) != 255) {
                            int yOffset = in.getUnsignedByte();
                            int type = in.getUnsignedByte();
                            addGameObject(new GameObject(objectId, type, baseX + xOffset, baseY + yOffset));
                        }
                    }
                }
            }
        } catch (IOException ioe) {
        }
    }
}